/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.FrameControlDanhMuc;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 10520_000
 */
public final class JIFSach extends javax.swing.JInternalFrame 
{

    DTO.Sach sach;
    /**
     * Creates new form JIFSach
     */
    public JIFSach()
    {
       
        initComponents();
    
        this.setSize(1000, 600);
    
        jTableSach.addMouseListener(new MouseAdapter(){  
            @Override
            public void mouseReleased(MouseEvent Me){  
                if(Me.isPopupTrigger()){  
                    jPopupMenu1.show(Me.getComponent(), Me.getX(), Me.getY()); 
                }  
            }  
        });    
        //..Tai thong tin sach vao table
        LoadData();

    }
 

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItemAdd = new javax.swing.JMenuItem();
        jMenuItemDel = new javax.swing.JMenuItem();
        jMenuItemSua = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItemLoad = new javax.swing.JMenuItem();
        jMenuItemTim = new javax.swing.JMenuItem();
        jMenuItemPrint = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenuItemNhap = new javax.swing.JMenuItem();
        jMenuItemXuat = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jButtonThemMoi = new javax.swing.JButton();
        jButtonSua = new javax.swing.JButton();
        jButtonXoa = new javax.swing.JButton();
        jButtonThoat = new javax.swing.JButton();
        jButtonXem = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSach = new javax.swing.JTable();

        jMenuItemAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add1.png"))); // NOI18N
        jMenuItemAdd.setText("Thêm mới");
        jPopupMenu1.add(jMenuItemAdd);

        jMenuItemDel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/del.png"))); // NOI18N
        jMenuItemDel.setText("Xóa");
        jPopupMenu1.add(jMenuItemDel);

        jMenuItemSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/sua.png"))); // NOI18N
        jMenuItemSua.setText("Sửa");
        jPopupMenu1.add(jMenuItemSua);
        jPopupMenu1.add(jSeparator1);

        jMenuItemLoad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/refresh.png"))); // NOI18N
        jMenuItemLoad.setText("Tải lại");
        jPopupMenu1.add(jMenuItemLoad);

        jMenuItemTim.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/search1.png"))); // NOI18N
        jMenuItemTim.setText("Tìm kiếm");
        jPopupMenu1.add(jMenuItemTim);

        jMenuItemPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/print.png"))); // NOI18N
        jMenuItemPrint.setText("In ấn");
        jPopupMenu1.add(jMenuItemPrint);
        jPopupMenu1.add(jSeparator2);

        jMenuItemNhap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/input.png"))); // NOI18N
        jMenuItemNhap.setText("Nhập từ Excel");
        jPopupMenu1.add(jMenuItemNhap);

        jMenuItemXuat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/excel.png"))); // NOI18N
        jMenuItemXuat.setText("Xuất ra Excel");
        jPopupMenu1.add(jMenuItemXuat);

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Danh sách Sách");

        jPanel1.setBackground(new java.awt.Color(49, 122, 233));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jButtonThemMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add1.png"))); // NOI18N
        jButtonThemMoi.setText("Thêm mới");
        jButtonThemMoi.setPreferredSize(new java.awt.Dimension(100, 25));
        jButtonThemMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThemMoiActionPerformed(evt);
            }
        });

        jButtonSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/sua.png"))); // NOI18N
        jButtonSua.setText("Hiệu chỉnh");
        jButtonSua.setEnabled(false);
        jButtonSua.setPreferredSize(new java.awt.Dimension(100, 25));
        jButtonSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSuaActionPerformed(evt);
            }
        });

        jButtonXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/del.png"))); // NOI18N
        jButtonXoa.setText("Xóa");
        jButtonXoa.setEnabled(false);
        jButtonXoa.setMaximumSize(new java.awt.Dimension(100, 25));
        jButtonXoa.setPreferredSize(new java.awt.Dimension(100, 25));
        jButtonXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXoaActionPerformed(evt);
            }
        });

        jButtonThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/close.png"))); // NOI18N
        jButtonThoat.setText("Thoát");
        jButtonThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThoatActionPerformed(evt);
            }
        });

        jButtonXem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/search.png"))); // NOI18N
        jButtonXem.setText("Tìm kiếm");
        jButtonXem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonThemMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonSua, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonXem, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(140, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonThemMoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonSua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonXoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonThoat)
                    .addComponent(jButtonXem))
                .addContainerGap())
        );

        jTableSach.setAutoCreateRowSorter(true);
        jTableSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Mã Sách", "Tên sách", "Thể loại", "Tác giả", "Số lượng tồn"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableSach.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableSach.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableSachMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableSach);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonThemMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThemMoiActionPerformed
        // TODO add your handling code here
        jframeThemSach frm = new jframeThemSach(this);
        
        frm.show();
       
    }//GEN-LAST:event_jButtonThemMoiActionPerformed

    private void jButtonXemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXemActionPerformed
        // TODO add your handling code here:
        jframeTimSach frm = new jframeTimSach();
        frm.show();
    }//GEN-LAST:event_jButtonXemActionPerformed

    private void jButtonSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSuaActionPerformed
        // TODO add your handling code here:
        Object O = ((DefaultTableModel)jTableSach.getModel()).getValueAt(jTableSach.getSelectedRow(), 1);
        new jframeSuaSach((String)O, this).show();
    }//GEN-LAST:event_jButtonSuaActionPerformed

    private void jButtonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXoaActionPerformed
        // TODO add your handling code here:
        int index = jTableSach.getSelectedRow();
        DefaultTableModel def = (DefaultTableModel)jTableSach.getModel();
        if(!new DTO.Sach(def.getValueAt(index, 1).toString(),"","","","1").Delete())
            JOptionPane.showMessageDialog(this, "Không thể Xóa", "Lỗi", JOptionPane.ERROR_MESSAGE);
        else
            LoadData();
    }//GEN-LAST:event_jButtonXoaActionPerformed

    private void jButtonThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonThoatActionPerformed

    private void jTableSachMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableSachMouseClicked
        // TODO add your handling code here:
        if(jTableSach.getSelectedColumn() != -1)
        {
            jButtonSua.setEnabled(true);
            jButtonXoa.setEnabled(true);
        }
    }//GEN-LAST:event_jTableSachMouseClicked

    public void LoadData()
    {
        TOOL.Ulti.CleanTabe(jTableSach);
        new BUS.Sach().LoadData((Object)jTableSach);
        if(jTableSach.getSelectedColumn() != -1)
        {
            jButtonSua.setEnabled(true);
            jButtonXoa.setEnabled(true);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSua;
    private javax.swing.JButton jButtonThemMoi;
    private javax.swing.JButton jButtonThoat;
    private javax.swing.JButton jButtonXem;
    private javax.swing.JButton jButtonXoa;
    private javax.swing.JMenuItem jMenuItemAdd;
    private javax.swing.JMenuItem jMenuItemDel;
    private javax.swing.JMenuItem jMenuItemLoad;
    private javax.swing.JMenuItem jMenuItemNhap;
    private javax.swing.JMenuItem jMenuItemPrint;
    private javax.swing.JMenuItem jMenuItemSua;
    private javax.swing.JMenuItem jMenuItemTim;
    private javax.swing.JMenuItem jMenuItemXuat;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JTable jTableSach;
    // End of variables declaration//GEN-END:variables
}
